<template>
    <div>
        <top-simple-search @searchFun="query" @addFun="handleAdd">
            <el-form-item label="" v-if="!isFaqi&&!isDraft">
            <el-input v-model="searchParams.alias" size="small" :style="{width: '200px'}" placeholder="请输入拟稿人" clearable></el-input>
            </el-form-item>
            <el-form-item label="" v-if="!isFaqi&&!isDraft">
                <dept-select-tree v-model="searchParams.dept_id" class="treeselect" size="small" placeholder="请选择拟稿人部门"></dept-select-tree>
            </el-form-item>
            <el-form-item label="" v-if="!isDraft&&!isTodo">
            <el-input placeholder="当前办理人" v-model="searchParams.participant" size="small" style="width: 200px;" clearable></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-date-picker
                        v-model="time"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        size="small"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <template v-if="params.type_id==3&&!appFun">
                <el-form-item label="">
                    <el-autocomplete
                            class="inline-input"
                            v-model="searchParams.fwh"
                            :fetch-suggestions="querySearch"
                            placeholder="请输入发文号"
                            @select="handleSelect"
                            :style="{width: '45%'}"
                    ></el-autocomplete>
                    〔
                    <el-input v-model="searchParams.year" placeholder="" :style="{width: '80px'}" clearable
                              class="fwh"></el-input>
                    〕
                    <el-input v-model="searchParams.fwz" placeholder="" :style="{width: '80px'}" clearable
                              class="fwh"></el-input>
                    号
                </el-form-item>
            </template>
            <template v-if="params.type_id==2">
                  <el-form-item label="" prop="num">
                    〔
                    <el-input v-model="searchParams.year" placeholder="年" :style="{width: '80px'}"
                              class="fwh"></el-input>
                    〕
                    <el-input v-model="searchParams.num" placeholder="归档编号" :style="{width: '90px'}"
                              class="fwh" ></el-input>
                    <!--        <el-input v-model="formData.collect_txt" placeholder="请输入收文文号" clearable :style="{width: '100%'}" :disabled="disabled"></el-input>-->
                  </el-form-item>
            </template>
        </top-simple-search>
        <simple-table api-url="officialDocument/schoolSignature" :api-fun="appFun" ref="myTable" :params="params" v-on="$listeners"  v-bind="$attrs" @loadComplete="loadComplete">
            <el-table-column align="left" prop="title" label="标题" min-width="200">
                <template slot-scope="{row}">
                    <span style="color:red" v-if="row.is_send">【抄送】</span><span style="color:red" v-if="row.urge==1&&(isTodo||params.urge==1)">【催办】</span><my-link @to="toDetail(row)">{{row.title}}</my-link>
                </template>
            </el-table-column>
            <el-table-column align="left" prop="level" label="紧急程度" v-if="params.type_id==2"  width="80">
                <template slot-scope="{row}">
<!--                    <span v-if="row.level==2" style="color:red">特急</span>-->
                    <span v-if="row.level==1" style="color:red">紧急</span>
                    <span v-else>一般</span>
                </template>
            </el-table-column>
            <el-table-column align="left" prop="collect_txt" label="收文号" width="200"  v-if="params.type_id==2">
            </el-table-column>
            <el-table-column align="left" prop="dept_info.name" label="发文号" width="200" v-else-if="params.type_id==3">
              <template slot-scope="{row}">
                <span v-if="row.fwh">{{row.fwh+"〔"+row.year+"〕 "+(row.fwz?"":row.fwz)}}号</span>
              </template>
            </el-table-column>
            <el-table-column align="left" prop="alias" :label="lableName" width="100">
            </el-table-column>
            <el-table-column align="left" prop="phone" label="手机" width="120" v-if="params.type_id==3">
            </el-table-column>
            <el-table-column align="left" prop="dept_info.name" :label="lableName+'部门'" min-width="180">
            </el-table-column>
            <el-table-column align="left" prop="participant" label="当前办理人" width="120" v-if="params.category=='owner'||params.category=='relation'">
                <template slot-scope="{row}">
                    <span  v-if="row.participant">{{row.participant}}</span>
                    <span v-else-if="row.act_state_id==4">
                        已办结
                    </span>
                </template>
            </el-table-column>
            <el-table-column align="left" prop="unread" label="是否已读" v-if="appFun" :formatter="unreadFormatter">
            </el-table-column>
            <el-table-column align="left" prop="handle_date" :label="params.category=='duty'?'流转时间':'办理时间'" width="170"  v-if="params.category=='relation'||params.category=='duty'">
            </el-table-column>
            <el-table-column align="left" prop="gmt_created" label="提交时间" width="170" v-else-if="params.category=='owner'||params.is_draft==1">
            </el-table-column>
            <el-table-column align="left" prop="gmt_modified" label="办理时间" width="170" v-else>
            </el-table-column>
            <!--            <el-table-column align="left" prop="recorder.alias" label="前序办理人"/>-->
            <el-table-column align="left" fixed="right" label="操作" width="200">
                <template slot-scope="{row}">
                    <edit-button v-if="(isDraft||row.is_edit&&isTodo)"  @click="toEdit(row)"></edit-button>
                    <my-button handle="chuli" v-else-if="isTodo" @click="toHandle(row)">处理</my-button>
                    <detail-button v-else @click="toView(row)"></detail-button>
                    <my-button handle="cuiban" v-if="isShowUrgeBtn(row)" @click="urge(row)">催办</my-button>
                    <my-button handle="Level" @click="toChangeLevel(row)">优先级</my-button>
                    <my-button handle="destroy" @click="toDestory(row)" v-if="isFaqi">销毁</my-button>
                    <delete-button v-if="isDraft"  @click="toDelete(row.id)"></delete-button>
                    <my-button handle="huizhi"  @click="showCountDialog(row)" v-if="showCyhz(row)">传阅回执</my-button>
                    <my-button handle="cancel"  @click="toCancel(row)" v-if="isFaqi">取消</my-button>
                    <my-button handle="retreat"  @click="setTicketsRetreat(row)" v-if="isFaqi">撤回</my-button>
                    <my-button handle="shezhi"  @click="showSettingDialog(row)" v-if="isTodo">设置</my-button>
                </template>
            </el-table-column>
        </simple-table>
        <level-dialog :visible.sync="visible" v-if="visible" :row="currRow" @reloadList="query"></level-dialog>
        <count-detail-dialog :id="currRow.id" :visible.sync="countVisible" v-if="countVisible" :type="currRow.type_id"></count-detail-dialog>
        <document-setting-dialog :visible.sync="settingVisible" v-if="settingVisible"  :id="currRow.id"></document-setting-dialog>
    </div>
</template>
<script>
    import {handleTree} from "@/utils/common";
    import schoolSignature from "@/api/officialDocument/schoolSignature/index"
    import schoolDocuments from "@/api/officialDocument/schoolDocuments/index"
    import MyLink from "../../../components/MyLink/index";
    import LevelDialog from "./LevelDialog";
    import CountDetailDialog from "./CountDetailDialog";
    import eventBus from "@/utils/eventBus";
    import user from "@/api/systemManagement/user/index";
    import DeptSelectTree from "@/components/Common/DeptSelectTree";
    import DocumentSettingDialog from "./DocumentSettingDialog";
    import ExtendTable from "@/mixins/ExtendTable";
    export default {
        name: "ListTable",
        components: {DocumentSettingDialog, DeptSelectTree, CountDetailDialog, LevelDialog, MyLink},
        props: ["params","appFun"],
      mixins:[ExtendTable],
        data() {
            return {
                time:["",""],
                visible: false,
                currRow:{},
                searchParams:{fwh:"",year:undefined,fwz:"",alias:"",starttime:"",endtime:"",search_value:"",dept:undefined,participant:undefined,num:undefined},
                fwhList:[],
                countVisible:false,
                settingVisible:false
            }
        },
        created() {
            if(this.params.type_id==3){
              //  this.searchParams.year = new Date().getFullYear()
                this.getDocumentCategory()
            }
        },
        computed: {
            isDraft() {
                return this.params.is_draft
            },
            isTodo() {
                return this.params.category=='duty'
            },
            lableName(){
                return this.appFun?"发送人":"拟稿人"
            },
            isYiban() {
                return this.params.category=='relation'
            },
            isFaqi() {
                return this.params.category=='owner'
            },
        },
        methods: {
            showSettingDialog(row){
                this.currRow = row
                this.settingVisible = true
            },
            showCyhz(row){
                return row.is_dist&&!this.isTodo&&row.creator==sessionStorage.getItem("username")
            },
            toDestory(row){
                this.$confirm("销毁后将不可恢复, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    schoolSignature.documentDels(row.id).then(res=>{
                        this.msgSuccess(res.msg)
                        this.query();
                    })
                });
            },
            querySearch(queryString, cb) {
                schoolDocuments.getDocumentCategory({search_value:queryString}).then(res => {
                    this.fwhList = res.data.map(item=>{
                        return {value:item.name}
                    })
                    cb(this.fwhList);
                })
            },
            handleSelect(item) {
            },
            showCountDialog(row){
                this.currRow = row
                this.countVisible = true
            },
            unreadFormatter(row){
                if(row.unread){
                    return "未读"
                }else{
                    return "已读"
                }
            },
            toDetail(row){
                if(this.isDraft || row.is_edit&&this.isTodo){
                    this.toEdit(row)
                }else if(this.isTodo){
                    this.toHandle(row)
                }
                // else if(this.isYiban&&!row.is_view){
                //     this.msgError("其它部门公文，您无权查看！")
                // }
                else{
                    this.toView(row)
                }
            },
            isShowUrgeBtn(row){
                return this.params.category=='owner'&&row.type_id==1 &&row.act_state_id!=4
            },
            urge(row){
                schoolSignature.urge(row.id).then(res=>{
                    this.msgSuccess(res.msg)
                    row.urge=1
                })
            },
            toHandle(row){
                this.$emit("showDialog",row,"handle")
            },
            toView(row){
                this.$emit("showDialog",row,"view")
            },
            handleAdd() {
                this.$emit("showDialog",{},"add")
            },
            toEdit(row){
                this.$emit("showDialog",row,"edit")
            },
            query(queryData) {
                this.searchParams.search_value=queryData
                if(this.params.type_id == 3){
                    this.searchParams.starttime = this.time[0]
                    this.searchParams.endtime = this.time[1]
                }
                this.$refs["myTable"].searchList(this.searchParams);
            },
            getList() {
                this.$refs["myTable"].getList();
            },
            toDelete(id) {
                this.$confirm("删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    schoolSignature.deleteDocument(id).then(res => {
                        this.query();
                        this.msgSuccess(res.msg);
                    });
                });
            },
            toChangeLevel(row){
                this.currRow=row
                this.visible=true
            },
            getDocumentCategory() {
                schoolDocuments.getDocumentCategory().then(res => {
                    this.fwhList = res.data
                })
            },
            loadComplete(data){
                if(this.isTodo){
                    eventBus.$emit("setToDoNum",data.total)
                }else if (this.appFun&&this.params.unread){
                    eventBus.$emit("setToReadNum",data.total)
                }
            }
        }
    }
</script>
<style lang="scss" scoped>
    .fwh {
        ::v-deep.el-input__inner {
            padding: 0 8px;
        }
        ::v-deep.el-input__suffix{
            right: 0px;
        }
    }

</style>
